﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using Maticsoft.DBUtility;
using System.Data.SqlClient;

namespace ElingOA6_Admin_DSJCJ.DAL
{
    /// <summary>
    /// ItemCls业务逻辑层
    /// </summary>
    public partial class ItemCls
    {
        /// <summary>
        /// 查询数据列表
        /// </summary>
        /// <creator>chenly</creator>
        /// <date>2013-9-22</date>
        /// <param name="strWhere">where条件</param>
        /// <returns>数据表</returns>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select FItemClsNo,FEntityNo,FItemClsName ");
            strSql.Append(" FROM dbo.t_ItemCls ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where FEntityNo=" + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 查询数据列表
        /// </summary>
        /// <creator>zhongyl</creator>
        /// <date>2013-9-29</date>
        /// <param name="strWhere">where条件</param>
        /// <returns>数据表</returns>
        public DataSet GetListAll(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select FItemClsNo,FEntityNo,FItemClsName ");
            strSql.Append(" FROM dbo.t_ItemCls ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 数据列表
        /// </summary>
        /// <param name="entityNo">公司编号</param>
        /// <returns>DataSet表</returns>
        public DataSet GetListByClsNo(string entityNo)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select FItemClsNo,FEntityNo,FItemClsName ");
            strSql.Append(" FROM dbo.t_ItemCls  where FEntityNo=@FEntityNo");
            strSql.Append(" order by FItemClsNo asc");

            SqlParameter[] parameters = {
                    new SqlParameter("@FEntityNo",SqlDbType.NVarChar,15)};

            parameters[0].Value = entityNo;

            return DbHelperSQL.Query(strSql.ToString(), parameters);
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        /// <param name="model">model对象</param>
        /// <returns>是否添加</returns>
        public bool Add(ElingOA6_Admin_DSJCJ.Model.ItemCls model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into dbo.t_ItemCls(");
            strSql.Append("FItemClsNo,FEntityNo,FItemClsName)");
            strSql.Append(" values (");
            strSql.Append("@FItemClsNo,@FEntityNo,@FItemClsName)");
            SqlParameter[] parameters = {
					new SqlParameter("@FItemClsNo", SqlDbType.NVarChar,6),
					new SqlParameter("@FEntityNo", SqlDbType.NVarChar,12),
					new SqlParameter("@FItemClsName", SqlDbType.NVarChar,20)};
            parameters[0].Value = model.FItemClsNo;
            parameters[1].Value = model.FEntityNo;
            parameters[2].Value = model.FItemClsName;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        /// <param name="model">model对象</param>
        /// <returns>是否更新</returns>
        public bool Update(ElingOA6_Admin_DSJCJ.Model.ItemCls model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update dbo.t_ItemCls set ");
            strSql.Append("FItemClsName=@FItemClsName");
            strSql.Append(" where FItemClsNo=@FItemClsNo and FEntityNo=@FEntityNo ");
            SqlParameter[] parameters = {
					new SqlParameter("@FItemClsName", SqlDbType.NVarChar,20),
					new SqlParameter("@FItemClsNo", SqlDbType.NVarChar,6),
					new SqlParameter("@FEntityNo", SqlDbType.NVarChar,12)};
            parameters[0].Value = model.FItemClsName;
            parameters[1].Value = model.FItemClsNo;
            parameters[2].Value = model.FEntityNo;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 得到一个实体对象
        /// </summary>
        /// <param name="itemClsNo">用品编号</param>
        /// <param name="entityNo">公司编号</param>
        /// <returns>得到一个实体对象</returns>
        public ElingOA6_Admin_DSJCJ.Model.ItemCls GetModel(string itemClsNo, string entityNo)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 FItemClsNo,FEntityNo,FItemClsName from dbo.t_ItemCls ");
            strSql.Append(" where FItemClsNo=@FItemClsNo and FEntityNo=@FEntityNo ");
            SqlParameter[] parameters = {
					new SqlParameter("@FItemClsNo", SqlDbType.NVarChar,6),
					new SqlParameter("@FEntityNo", SqlDbType.NVarChar,12)			};
            parameters[0].Value = itemClsNo;
            parameters[1].Value = entityNo;

            ElingOA6_Admin_DSJCJ.Model.ItemCls model = new ElingOA6_Admin_DSJCJ.Model.ItemCls();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                return DataRowToModel(ds.Tables[0].Rows[0]);
            }
            else
            {
                return null;
            }
        }
        /// <summary>
        /// 是否存在该记录
        /// </summary>
        /// <param name="itemClsNo">用品编号</param>
        /// <param name="entityNo">公司编号</param>
        /// <returns>是否存在</returns>
        public bool Exists(string itemClsNo, string entityNo)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from dbo.t_ItemCls");
            strSql.Append(" where FItemClsNo=@FItemClsNo and FEntityNo=@FEntityNo ");
            SqlParameter[] parameters = {
					new SqlParameter("@FItemClsNo", SqlDbType.NVarChar,6),
					new SqlParameter("@FEntityNo", SqlDbType.NVarChar,12)			};
            parameters[0].Value = itemClsNo;
            parameters[1].Value = entityNo;

            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        /// <param name="itemClsNo">用品编号</param>
        /// <param name="entityNo">公司编号</param>
        /// <returns>是否删除</returns>
        public bool Delete(string itemClsNo, string entityNo)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from dbo.t_ItemCls ");
            strSql.Append(" where FItemClsNo=@FItemClsNo and FEntityNo=@FEntityNo ");
            SqlParameter[] parameters = {
					new SqlParameter("@FItemClsNo", SqlDbType.NVarChar,6),
					new SqlParameter("@FEntityNo", SqlDbType.NVarChar,12)			};
            parameters[0].Value = itemClsNo;
            parameters[1].Value = entityNo;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 得到一个实体对象
        /// </summary>
        /// <param name="row">表格行</param>
        /// <returns>model对象</returns>
        public ElingOA6_Admin_DSJCJ.Model.ItemCls DataRowToModel(DataRow row)
        {
            ElingOA6_Admin_DSJCJ.Model.ItemCls model = new ElingOA6_Admin_DSJCJ.Model.ItemCls();
            if (row != null)
            {
                if (row["FItemClsNo"] != null)
                {
                    model.FItemClsNo = row["FItemClsNo"].ToString();
                }
                if (row["FEntityNo"] != null)
                {
                    model.FEntityNo = row["FEntityNo"].ToString();
                }
                if (row["FItemClsName"] != null)
                {
                    model.FItemClsName = row["FItemClsName"].ToString();
                }
            }
            return model;
        }
    }
}
